/**
 * 
 */
package com.lorizz.picpub.task.impl;

import java.awt.image.BufferedImage;

import com.lorizz.picpub.services.ImageService;
import com.lorizz.picpub.services.ImageStore;
import com.lorizz.upm.ImageTransformTask;
import com.lorizz.upm.ImageVariant;

/**
 * @author wind hong
 * 
 */
public class ImageTransformTaskExecutor extends AbstractTaskExecutor<ImageTransformTask> {

    private ImageService imageService;
    private ImageStore imageStore;
    private String storeCategory;

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public void setImageStore(ImageStore imageStore) {
        this.imageStore = imageStore;
    }
    
    public void setStoreCategory(String storeCategory) {
		this.storeCategory = storeCategory;
	}

    @Override
    protected void executeTask(ImageTransformTask task) {
        BufferedImage ori = imageStore.load(task.getImage().getUri());
        BufferedImage scaled = imageService.scale(ori, task.getMaxWidth(), task.getMaxHeight());
        String uri = imageStore.store(storeCategory, scaled);
        ImageVariant variant = task.getImage().createImageVariant();
        variant.setWidth(scaled.getWidth());
        variant.setHeight(scaled.getHeight());
        variant.setName(task.getName());
        variant.setUri(uri);
        upmRepository.persist(variant);
    }

}
